package com.ffyc.cms.controller;

import com.ffyc.cms.common.CommonResult;
import com.ffyc.cms.model.User;
import com.ffyc.cms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping(path = "/login")
public class UserController {
    @Autowired
    UserService userService;


    @PostMapping(path = "/updatePwd")
    public CommonResult updatePwd(User user, HttpSession session) {
        CommonResult commonResult = null;
        try {
            //账号唯一，依据账号修改密码
            User user1 = (User) session.getAttribute("user");
            user.setAccount(user1.getAccount());
            userService.updateUserPwd(user);
            commonResult = new CommonResult(200, "修改成功", user);
        } catch (Exception e) {
            e.printStackTrace();
            commonResult = new CommonResult(500, "修改失败", null);
        }
        return commonResult;
    }

    /**
     * 查询原密码是否正确
     *
     * @param user
     * @return
     */
    @PostMapping(path = "/checkPwd")
    public CommonResult checkPwd(User user) {
        CommonResult commonResult = null;
        try {
            User user1 = userService.checkPwd(user);
            if (user1 == null) {
                commonResult = new CommonResult(201, "密码错误,请重新输入", null);
            } else {
                commonResult = new CommonResult(200, "密码正确", user1);
            }
        } catch (Exception e) {
            e.printStackTrace();
            commonResult = new CommonResult(500, "服务器忙，请稍后再试！！", null);
        }
        return commonResult;
    }

    /**
     * 修改用户个人信息
     *
     * @param user
     * @return
     */
    @PostMapping(path = "/updateMsg")
    public CommonResult updateMsg(User user, HttpSession session) {
        CommonResult commonResult = null;
        try {
            User user1 = (User) session.getAttribute("user");
            user.setAccount(user1.getAccount());
            userService.updateUserMsg(user);
            commonResult = new CommonResult(200, "修改成功", user);
        } catch (Exception e) {
            e.printStackTrace();
            commonResult = new CommonResult(500, "修改失败", null);
        }
        return commonResult;
    }

    /**
     * 查询用户个人信息
     *
     * @param
     * @return
     */
    @RequestMapping(path = "/findUserMsg")
    public CommonResult findUserMsg(String account) {
        CommonResult commonResult = null;
        try {
            User user = userService.findUserMsg(account);
            commonResult = new CommonResult(200, "查询成功", user);
        } catch (Exception e) {
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败", null);
        }
        return commonResult;
    }

    /**
     * 用户注册
     * 修改用户个人信息
     * <p>
     * 有问题未修改，需要分开修改和注册
     *
     * @return
     */
    @PostMapping(path = "/register")
    public CommonResult save(User user, HttpSession session) {
        CommonResult commonResult = null;
        try {
            userService.saveUser(user);
            commonResult = new CommonResult(200, "注册成功", user);
        } catch (Exception e) {
            e.printStackTrace();
            commonResult = new CommonResult(500, "注册失败", null);
        }
        return commonResult;
    }


    //登录
    @PostMapping(path = "/login")
    public CommonResult login(User user, HttpSession session) {
        CommonResult commonResult = null;
        try {
            System.out.println("走到这里了");
            User user1 = userService.login(user);
            if (user1 == null) {
                commonResult = new CommonResult<User>(201, "账号或密码错误", null);
            } else {
                session.setAttribute("user", user1);
                commonResult = new CommonResult<User>(200, "登录成功", user1);
            }
        } catch (Exception e) {
            e.printStackTrace();
            commonResult = new CommonResult<User>(500, "登录失败", null);
        }
        return commonResult;
    }

    //安全退出
    @GetMapping(path = "/loginOut")
    public void loginOut(HttpSession session) {
        session.invalidate();
    }


    /**
     * 获取用户列表
     * @return
     */
    @RequestMapping(path = "getUserList")
    public CommonResult getUserList(){
        CommonResult commonResult = null;
        try {
            List<User> userList = userService.getUserList();
            commonResult = new CommonResult(200, "登录成功",  userList);
        } catch (Exception e) {
            e.printStackTrace();
            commonResult = new CommonResult(500, "服务器忙，请稍后重试！", null);
        }
        return commonResult;
    }
    @RequestMapping(path = "updateaccstate")
    public CommonResult updateaccstate(Integer id,Integer accstate){
        CommonResult commonResult;
        try {
            userService.updateaccstate(id,accstate);
            commonResult = new CommonResult(200, "修改状态成功",  null);
        } catch (Exception e) {
            e.printStackTrace();
            commonResult = new CommonResult(500, "服务器忙，请稍后重试！", null);
        }
        return commonResult;
    }
}
